﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Ekton_Manage_Framework.Methods
{
    public class ExpressionMapper
    {
        /// <summary>
        /// 可以处理复杂映射
        /// </summary>
        /// <typeparam name="TIn">输入类</typeparam>
        /// <typeparam name="TOut">输出类</typeparam>
        /// <param name="expression">表达式目录树,可以为null</param>
        /// <param name="tIn">输入实例</param>
        /// <returns></returns>
        public static TOut Mapper<TIn, TOut>(TIn tIn, Expression<Func<TIn, TOut>> expression = null)
        {
            ParameterExpression parameterExpression = null;
            List<MemberBinding> memberBindingList = new List<MemberBinding>();
            parameterExpression = Expression.Parameter(typeof(TIn), "p");

            if (expression != null)
            {
                parameterExpression = expression.Parameters[0];
                if (expression.Body != null)
                {
                    memberBindingList.AddRange((expression.Body as MemberInitExpression).Bindings);
                }
            }
            foreach (var item in typeof(TOut).GetProperties())
            {
                if (typeof(TIn).GetProperty(item.Name) != null)
                {
                    MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingList.Add(memberBinding);
                }
                if (typeof(TIn).GetField(item.Name) != null)
                {
                    MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingList.Add(memberBinding);
                }
            }
            foreach (var item in typeof(TOut).GetFields())
            {
                if (typeof(TIn).GetField(item.Name) != null)
                {
                    MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingList.Add(memberBinding);
                }
                if (typeof(TIn).GetProperty(item.Name) != null)
                {
                    MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingList.Add(memberBinding);
                }
            }
            MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());

            Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
            {
                    parameterExpression
            });
            Func<TIn, TOut> func = lambda.Compile();//获取委托
            return func.Invoke(tIn);
        }
    }
}
